package com.liang.demo.service.impl;

import com.liang.demo.config.cache.RedisConfig;
import com.liang.demo.mapper.RoleMapper;
import com.liang.demo.po.Role;
import com.liang.demo.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author 陈琪亮
 * @version 1.0
 * @date 2021/3/21 10:51
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Role> getRoleList(Long adminId) {
        return roleMapper.getRoleList(adminId);
    }

    @Override
    @Cacheable(value = RedisConfig.REDIS_KEY_DATABASE,key = "'role:'+#adminId")
    public Set<String> getRolesByAdminId(String adminId) {
        List<Role> roles = roleMapper.getRoleList(Long.parseLong(adminId));
        Set<String> rolesStr = new HashSet<>();
        roles.forEach( r -> rolesStr.add(r.getName()) );
        return rolesStr;
    }

    @Override
    public List<Role> getRoleAll() {
        List<Role> roles = roleMapper.getRoleAll();
        return roles;
    }
}
